//定时器
var maotaiUrl = 'https://item.jd.com/100012043978.html';
var scopeStr = 'jd.com';
var pound = '#';
var homeUrl = 'https://www.jd.com/';

//检测状态
function checkElementState(path, callback) {
    let ele = document.querySelector(path);
    if (ele) {
        callback && callback();
    } else {
        console.log('异步加载元素中....' + path);
        setTimeout(function () {
            checkElementState(path, callback);
        }, 200);
    }
}

//放入购物车
function putCart() {

    try{
        const resBtn = document.getElementById('btn-reservation');
        let dDate = new Date();
        dDate.setHours(9, 59, 59.2);
        const date = new Date();

        //当前时间
        let currentHour = date.getHours();
        let currentMin = date.getMinutes();
        console.log('currentHour=' + currentHour + ',currentMin=' + currentMin);
        if (currentHour != 9 && currentHour != 10) {
            window.localStorage.make = false;
            //模拟真人浏览网页
            setTimeout(function () {
                simulate();
            }, 900000);
        } else {
            //模拟刷新页面
            refreshLoc(date);
            let diff = Date.parse(dDate) - Date.parse(date);
            //console.log('diff=' + diff);
            if (resBtn) {
                //获得文字
                var resText = resBtn.innerText;
                console.log('resText=' + resText);
                if (resText.indexOf('预约') == -1 && diff > -2000) {
                    if (resText.indexOf('购') != -1) {
                        console.log('放入购物车....');
                        resBtn.click();
                    } else {
                        console.log('等待抢购....');
                        setTimeout(function () {
                            putCart();
                        }, 200);

                    }
                }
                //预约
                else if (resText.indexOf('开始预约') != -1 && currentMin == 35 && currentHour == 10) {
                    let storMake = window.localStorage.make;
                    console.log('storMake=' + storMake);
                    if (typeof (storMake) != 'undefined') {
                        //点击按钮
                        resBtn.click();
                        setTimeout(function () {
                            putCart();
                        }, 500);


                    } else {
                        window.localStorage.make = false;
                        setTimeout(function () {
                            putCart();
                        }, 200);
                    }


                } else if (diff < 500 && diff >= 0) {
                    //刷新当前页
                    window.location.reload();
                } else {
                    console.log('等待抢购....');
                    setTimeout(function () {
                        putCart();
                    }, 200);

                }
            } else {
                console.log('获取不到按钮....');
                //访问茅台预订页
                window.location.href = maotaiUrl;
            }
        }
    }catch (e) {
        console.error(e);
    }



}

//模拟浏览页面
function simulate() {

    try{
        //当前时间
        let date = new Date();

        //当前时间
        let currentHour = date.getHours();
        console.log("模拟浏览=" + currentHour);
        if (currentHour == 9 || currentHour == 10) {
            console.log("进入抢购=" + currentHour);
            window.location.href = maotaiUrl;
        } else {
            //获得所有连接
            let list = document.getElementsByTagName("a");
            if (list) {
                if (list.length > 0) {
                    let index = Math.floor((Math.random() * list.length));
                    let hrefUrl = list[index].href;
                    console.log("hrefUrl=" + hrefUrl);
                    //保证jd.com里面
                    if (hrefUrl.indexOf(scopeStr) > -1 && hrefUrl.indexOf(pound) < 0) {
                        window.location.href = hrefUrl;
                    } else {
                        window.location.href = homeUrl;
                    }
                } else {
                    window.location.href = maotaiUrl;
                }
            } else {
                window.location.href = maotaiUrl;
            }

        }
    }catch (e) {
        console.error(e);
    }




}

//购物车提交
function submitCart() {


}

//刷新当前页面
function refreshLoc(date) {

    //刷新网页17分钟5秒刷新一次，防止登陆丢失
    let minutes = date.getMinutes();
    let seconds = date.getSeconds();
    console.log('判断是否刷新页面=' + minutes + ',' + seconds);
    let refreshMin = minutes % 17;
    if (refreshMin == 0 && seconds == 5 && minutes != 0) {
        //刷新当前页面
        window.location.reload();
    }


}


//主要函数
function main() {
    console.log('############################开始抢购茅台############################');
    //当前时间
    let date = new Date();
    //debugger;

    let href = window.location.href;
    if (href.indexOf('item.jd.com') > -1) {
        //放入购物车
        putCart();


    } else if (href.indexOf('cart.jd.com') > -1) {
        //提交加购车订单
        setTimeout(function () {
            simulate();
        }, 900000);
    } else {

        //当前时间
        let currentHour = date.getHours();

        //模拟真人浏览网页
        console.log('模拟浏览页面' + currentHour);
        if (currentHour == 9 || currentHour == 10) {
            console.log("进入抢购=" + currentHour);
            window.location.href = maotaiUrl;
        } else {
            setTimeout(function () {
                simulate();
            }, 900000);
        }


    }

}


main();